Vue.filter('moment', function (value) {
    var value=value * 1000;
    var nowDate=Date.parse(new Date());
    var difference=nowDate - value;
    var second=60000; //表示一分钟
    if(difference < second){ //一分钟内
        return '刚刚';
    }else if(difference < second * 60){
        var days=Math.floor(difference / second);
        return days+'分钟前';
    }else if(difference < second * 1440){
        var days=Math.floor(difference/(second * 60));
        return days+'小时前';
    }else if(difference < second * 43200){
        var days=Math.floor(difference/(second * 60 * 24));
        return days+'天前';
    }else{
        var d = new Date(value);
        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        var day = d.getDate() <10 ? '0' + d.getDate() : '' + d.getDate();
        var hour = d.getHours();
        var minutes = d.getMinutes();
        return year+'-'+month+'-'+day+' '+hour+':'+minutes;
    }
});
function home_index(){
    var app = new Vue({
        el: '#app',
        data:{
            static:false,
            ismore:true,
            list:[],
            ajaxurl:"/index.php",
            pagesize:20,
            page:1,
            catid:"",
        },
        created: function () {
            this.$nextTick(function () {
                this.init(1);
            })
        },
        methods: {
            init:function(type){
                var $this=this;
                this.static=true;
                $.ajax({
                    type:"GET",
                    url:$this.ajaxurl,
                    dataType:"json",
                    data:{
                        m:"content",
                        c:"index",
                        a:"ajax_lists",
                        catid:$this.catid,
                        page:$this.page,
                        pagesize:$this.pagesize
                    },
                    success:function (data) {
                        $this.isHandle(type,data);
                    },
                    error:function (XMLHttpRequest, textStatus, errorThrown){
                        console.log(XMLHttpRequest);
                        app.static=false;
                        return false;
                    }
                });
                mySwiper($this.catid);
            },
            isHandle:function(type,data){
                var datas=[];
                if (data.length === 0) {
                    this.ismore=false;
                    return false;
                }
                var $this=this;
                $.each(data,function () {
                    if (this.thumb !== '') {
                        strs = this.thumb.split("|");
                        this.thumb = strs.slice(0, 4);
                        this.sizes=strs.length;
                    }else{
                        this.sizes=0;
                    }
                    if($this.catid === ''){
                        datas.push(this);
                    }else{
                        datas.unshift(this);
                    }
                })
                if(type===1){
                    this.list=datas;
                }else{
                    var $oldlist=this.list;
                    $.each(datas,function () {
                       $oldlist.push(this);
                    })
                }
                app.static=false;
            },
            isRefresh:function(){
                app.page=1;
                app.list=[];
                app.init(1);
            }
        }
    });
    $(window).scroll(function(){
        totalheight = parseFloat($(window).height()) + parseFloat($("body").scrollTop());
        if ($(document).height() <= totalheight && !app.static && app.ismore){
            app.page++;
            app.init(0);
        }
    });
    function mySwiper(){
        var mySwiper = new Swiper('#topNav', {
            freeMode: true,
            freeModeMomentumRatio: 0.5,
            slidesPerView:'auto',
        });
        var swiperWidth = mySwiper.container[0].clientWidth;
        var maxTranslate = mySwiper.maxTranslate();
        var maxWidth = -maxTranslate + swiperWidth / 2;
        $(".swiper-container").on('touchstart',function(e) {
            e.preventDefault();
        })
        mySwiper.on('click', function(swiper) {
            var slide = swiper.slides[swiper.clickedIndex];
            var slideLeft = slide.offsetLeft;
            var slideWidth = slide.clientWidth;
            var slideCenter = slideLeft + slideWidth / 2;
            mySwiper.setWrapperTransition(300)
            if (slideCenter < swiperWidth / 2) {
                mySwiper.setWrapperTranslate(0)
            } else if (slideCenter > maxWidth) {
                mySwiper.setWrapperTranslate(maxTranslate)
            } else {
                nowTlanslate = slideCenter - swiperWidth / 2
                mySwiper.setWrapperTranslate(-nowTlanslate)
            }
            $("#topNav .active").removeClass('active');
            $("#topNav .swiper-slide").eq(swiper.clickedIndex).addClass('active');
            var $cid=$("#topNav .swiper-slide").eq(swiper.clickedIndex).data("type");
            app.catid=$cid;
            app.page=1;
            app.list=[];
            app.init(1);
            return false;
        });
    };
}
function home_shoow($catid){
    var shows = new Vue({
        el: '#apps',
        data:{
            list:[],
            ajaxurl:"/index.php",
            pagesize:20,
            page:1,
            catid:$catid,
            ismore:false,
            static:true
        },
        created:function () {
            this.$nextTick(function () {
                this.init(1);
            })
        },
        methods: {
            init:function(type){
                /**if($("#content .TRS_Editor").length!=0){
                   var $hei=$("#content .TRS_Editor").height();
                }else{
                    var $hei=$("#content").height();
                }
                if($hei > 500){
                    console.log($hei);
                    this.ismore=true;
                    console.log(this.ismore);
                };**/
                var $this=this;
                this.static=false;
                $.ajax({
                    type:"GET",
                    url:$this.ajaxurl,
                    dataType:"json",
                    data:{
                        m:"content",
                        c:"index",
                        a:"ajax_lists",
                        catid:$this.catid,
                        page:$this.page,
                        pagesize:$this.pagesize
                    },
                    success:function (data) {
                        if (data.length === 0) {
                            $this.ismore=false;
                        }else{
                            $this.isHandle(type,data);
                        }
                    },
                    error:function (XMLHttpRequest, textStatus, errorThrown){
                        console.log(XMLHttpRequest);
                        return false;
                    }
                });
            },
            greet:function(){
                this.ismore=false;
            },
            isHandle:function(type,data){
                var datas=[];var $this=this;
                $.each(data,function () {
                    if (this.thumb !== '') {
                        strs = this.thumb.split("|");
                        this.thumb = strs.slice(0,3);
                        this.sizes=strs.length;
                    }else{
                        this.sizes=0;
                    }
                    datas.unshift(this);
                })
                if(type===1){
                    this.list=datas;
                }else{
                    var $oldlist=this.list;
                    $.each(datas,function () {
                       $oldlist.push(this);
                    })
                }
                this.static=true;
            },
        }
    });
    $(window).scroll(function(){
        totalheight = parseFloat($(window).height()) + parseFloat($("body").scrollTop());
        if ($(document).height() <= totalheight && shows.static && shows.ismore){
            shows.page++;
            shows.init(0);
        }
    });
};
